package dongtai;

public class sanlingjiu {
    public static int maxProfit(int[] prices) {
        int n=prices.length;
        if(n==1){
            return 0;
        }
        int[] f=new int[n];
        int[] g=new int[n];
        int[] m=new int[n];
        f[0]=-prices[0];
        g[0]=0;
        m[0]=0;
        for(int i=1;i<n;i++){
            f[i]=Math.max(m[i-1]-prices[i],f[i-1]);
            g[i]=f[i-1]+prices[i];
            m[i]=Math.max(g[i-1],m[i-1]);
        }
        return Math.max(f[n-1],Math.max(g[n-1],m[n-1]));

    }

    public static void main(String[] args) {
        int[] arr={2,1};
        int i = maxProfit(arr);
        System.out.println(i);

    }
}
